﻿using System;
using NHibernate.Validator.Constraints;
using System.ComponentModel;
using System.Collections.Generic;

namespace DataStore.Domain
{
    /// <summary>
    /// Extra content for document
    /// </summary>
    [Serializable]
    public class Extra : IEquatable<Extra>, IDataErrorInfo
    {
        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>The id.</value>
        public virtual Guid Id { get; set; }
        /// <summary>
        /// Gets or sets the descriptive text.
        /// </summary>
        /// <value>The text.</value>
        [NotNullNotEmpty]
        public virtual string Text { get; set; }
        /// <summary>
        /// Gets or sets the icon.
        /// </summary>
        /// <value>The icon.</value>
        public virtual Link Icon { get; set; }

        #region ToString

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return Text;
        }

        #endregion

        #region Error

        protected List<string> FieldsForValidation = new List<string> { "Text" };

        /// <summary>
        /// Gets an error message indicating what is wrong with this object.
        /// </summary>
        /// <value></value>
        /// <returns>An error message indicating what is wrong with this object. The default is an empty string ("").</returns>
        public virtual string Error
        {
            get
            {
                string error = string.Empty;

                foreach (string field in FieldsForValidation)
                {
                    if (this[field] != string.Empty)
                    {
                        error += this[field] + "\n";
                    }
                }

                return error;
            }
        }

        /// <summary>
        /// Gets the <see cref="System.String"/> with the specified column name.
        /// </summary>
        /// <value></value>
        public virtual string this[string columnName]
        {
            get
            {
                string error = String.Empty;

                if (columnName == "Text")
                {
                    if (string.IsNullOrWhiteSpace(Text))
                    {
                        error = "Text can't be empty";
                    }
                }

                return error;
            }
        }

        #endregion

        #region Equality

        /// <summary>
        /// Equalses the specified other.
        /// </summary>
        /// <param name="other">The other.</param>
        /// <returns></returns>
        public virtual bool Equals(Extra other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return other.Id.Equals(Id);
        }

        /// <summary>
        /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
        /// </summary>
        /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
        /// <returns>
        /// 	<c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
        /// </returns>
        public override bool Equals(object obj)
        {
            return Equals(obj as Extra);

        }

        /// <summary>
        /// Returns a hash code for this instance.
        /// </summary>
        /// <returns>
        /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 
        /// </returns>
        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="left">The left.</param>
        /// <param name="right">The right.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(Extra left, Extra right)
        {
            return Equals(left, right);
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="left">The left.</param>
        /// <param name="right">The right.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(Extra left, Extra right)
        {
            return !Equals(left, right);
        }

        #endregion
    }
}
